﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using FANUC.DTO;

namespace FANUC.Controllers
{
    public class RobotMaintainController : ApiController
    {
        private FANUCDB db = new FANUCDB();

        /// <summary>
        /// GET: api/ApiRobot
        /// </summary>
        /// <returns>RobotListDtoOutput</returns>
        [ResponseType(typeof(RobotListDtoOutput))]
        public IHttpActionResult Get()
        {
            var robots = db.Robot.ToList();
            if (robots.Count <= 0)
            {
                return NotFound();
            }
            AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<Robot, RobotDto>().ForMember(dest=>dest.Robot_Type,src=>src.MapFrom(s=>s.Robot_Type.Type)));
            List<RobotDto> robotsDto = AutoMapper.Mapper.Map<List<RobotDto>>(robots);
            RobotListDtoOutput data = new RobotListDtoOutput();
            data.Robots = robotsDto;
            return Ok(data);
        }

        /// <summary>
        /// GET: api/ApiRobot/?pageNumber=2&pageSize=10
        /// </summary>
        /// <param name="pageNumber">Page Number</param>
        /// <param name="pageSize">Page Size</param>
        /// <returns></returns>
        [ResponseType(typeof(RobotListDtoOutput))]
        public IHttpActionResult Get(int pageNumber, int pageSize)
        {
            var robots = db.Robot.OrderByDescending(o => o.ID).Skip(pageNumber).Take(pageSize).ToList();
            if (robots.Count <= 0)
            {
                return NotFound();
            }
            AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<Robot, RobotDto>());
            List<RobotDto> robotsDto = AutoMapper.Mapper.Map<List<RobotDto>>(robots);
            RobotListDtoOutput data = new RobotListDtoOutput();
            data.Robots = robotsDto;
            return Ok(data);
        }

        [ResponseType(typeof(RobotDto))]
        public IHttpActionResult Get(string id)
        {
            var robot = db.Robot.Where(a => a.YH_No == id).FirstOrDefault();
            if (robot == null)
            {
                return NotFound();
            }
            AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<Robot, RobotDto>());
            RobotDto robotsDto = AutoMapper.Mapper.Map<RobotDto>(robot);

            return Ok(robotsDto);
        }

        [ResponseType(typeof(RobotDto))]
        public IHttpActionResult Post(RobotDto robot)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var robotToCreate = db.Robot.Where(a => a.YH_No == robot.YH_No).FirstOrDefault();
            if (robot != null)
            {
                throw new Exception("Already Exist");
            }
            AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<RobotDto, Robot>());
            robotToCreate = AutoMapper.Mapper.Map<Robot>(robot);
            db.Robot.Add(robotToCreate);
            db.SaveChanges();
            robot.ID = robotToCreate.ID;
            return Ok(robot);
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        public string Options()
        {
            return null;
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
